package com.android.versiondownloadlibrary;

import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;

/**
 * Created by $Liudan on 2018/6/1 0001.
 */
public class RangeWriteFileUtil {

    private static final String TAG = "DownLoadManager";
    private static String APK_CONTENTTYPE = "application/vnd.android.package-archive";
    private static String PNG_CONTENTTYPE = "image/png";
    private static String JPG_CONTENTTYPE = "image/jpg";
    private static String MP4_CONTENTTYPE = "video/mp4";
    private static String fileSuffix = "";
    private static File sFutureStudioIconFile;
    private static long sFileSize;
    private static String fileName = "whstywh";


    public static String getFileName() {
        return fileName;
    }

    public static File getFutureStudioIconFile() {
        return sFutureStudioIconFile;
    }

    public static void writeFile(final ResponseBody body, final Handler mHandler, final UpdateUi updateUi) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                if(body==null)return;
                Log.d(TAG, "contentType:>>>>" + body.contentType().toString());
                String type = body.contentType().toString();
                if (type.equals(APK_CONTENTTYPE)) {
                    fileSuffix = ".apk";
                } else if (type.equals(PNG_CONTENTTYPE)) {
                    fileSuffix = ".png";
                } else if (type.equals(JPG_CONTENTTYPE)) {
                    fileSuffix = ".jpg";
                } else if (type.equals(MP4_CONTENTTYPE)) {
                    fileSuffix = ".mp4";
                }

                String sPath = Environment.getExternalStorageDirectory() + File.separator + fileName + fileSuffix;
                Log.d(TAG, "path:>>>>11111111111111" + sPath);
                try {
                    // todo change the file location/name according to your needs
                    sFutureStudioIconFile = new File(sPath);
                    InputStream inputStream = null;
                    OutputStream outputStream = null;

                    try {
                        byte[] fileReader = new byte[4096];
                        //需要下载的文件总长度
                        if (sFileSize == 0) {
                            sFileSize = body.contentLength();
                        }
                        //已经下载的文件长度
//                        long fileSizeDownloaded = sFutureStudioIconFile.length();
                        int fileSizeDownloaded = 0;
                        inputStream = body.byteStream();
                        outputStream = new FileOutputStream(sFutureStudioIconFile, true);
//                progressBar.setMax((int) sFileSize);
                        while (true) {
                            int read = inputStream.read(fileReader);
                            if (read == -1) {
                                break;
                            }
                            outputStream.write(fileReader, 0, read);
                            fileSizeDownloaded += read;
//                    progressBar.setProgress((int) fileSizeDownloaded);
//                            Message message = new Message();
//                            message.what = 1;
//                            message.arg1 = (int) sFileSize;
//                            message.arg2 = (int) fileSizeDownloaded;
//                            mHandler.sendMessage(message);

                            updateUi.updateUI((int) sFileSize,fileSizeDownloaded);
//                            Log.d(TAG, "file download:111111111111111 " + fileSizeDownloaded + " of " + sFileSize);
                        }
                        outputStream.flush();

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }

                        if (outputStream != null) {
                            outputStream.close();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }



}
